package com.zyx.service;

import com.zyx.mapper.CollectMapper;
import com.zyx.pojo.Auto;
import com.zyx.pojo.Collect;
import com.zyx.pojo.Name;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

/**
  * @author zyx
  * @date 2022/1/12
  */
@Service
public class CollectService {
    @Resource
    private CollectMapper collectMapper;

    @Resource
    private AutoService autoService;

    /**
     * @Description: 查询到收藏量为前五的汽车ID并排序返回
     * @Author: zyx
     * @Date: 11:05 2022/2/12
     * @Name: queryByTen
     * @Param: [size]
     * @Return: java.util.List<java.util.Map.Entry<java.lang.Integer,java.lang.Integer>>
     */
    @Transactional(propagation = Propagation.REQUIRED,readOnly = true)
    public List<Name> queryByTen(){
        List<Name> list = collectMapper.selectByTen();
        for (Name n : list){
          n.setPriceMax(autoService.queryMax(n.getCarName()));
          n.setPriceMin(autoService.queryMin(n.getCarName()));
        }
        return list;
    }

    //新增收藏
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = {Exception.class})
    public int addCollect(Collect c){
        return collectMapper.insertSelective(c);
    }

    //取消收藏
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = {Exception.class})
    public int delCollect(Integer id,Integer autoId){
        return collectMapper.delCollect(id,autoId);
    }

    //根据用户id查询用户收藏车辆
    @Transactional(propagation = Propagation.REQUIRED,readOnly = true)
    public List<Name> queryByUserId(Integer userId){
        return collectMapper.queryByUserId(userId);
    }

    //判断用户是否收藏车型
    @Transactional(propagation = Propagation.REQUIRED,readOnly = true)
    public Collect queryCollect(Collect c){
        return collectMapper.queryCollect(c);
    }
}
